﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.Repositories
{
    public class ScreenCastRepository : RepositoryInterfaces.IScreenCastRepository
    {
        /// <summary>
        /// Get screen casts for movie at correct cinema
        /// </summary>
        /// <param name="movie">Movie to get screen casts for</param>
        /// <param name="cinema">Cinema to get screen casts from</param>
        /// <returns>Screen casts for movie at cinema</returns>
        /// <exception cref="ArgumentNullException">Movie is null, or no screen casts for provided movie</exception>
        /// <exception cref="Exception"></exception>
        public IList<ScreenCast> GetScreenCasts(Movie movie, Cinema cinema)
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return (from sc in _db.ScreenCasts
                            join mh in _db.MovieHalls
                                on sc.MovieHallId equals mh.Id
                            join c in _db.Cinemas
                                on mh.CinemaId equals c.Id
                            where sc.MovieId == movie.Id
                                && c.Id == cinema.Id
                            select sc).ToList();
                }
            }
            catch (ArgumentNullException e)
            {
                throw new ArgumentNullException("Movie is null, or there exists no screen casts for provided movie" + Environment.NewLine + e.Message);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Add a screenCast to db
        /// </summary>
        /// <param name="screenCast">screenCast</param>
        /// <exception cref="ArgumentException">If provided screen cast object is null</exception>
        /// <exception cref="Exception"></exception>
        public void AddScreenCast(ScreenCast screenCast)
        {
            try
            {
                if (screenCast != null)
                {
                    using (BalderEntities _db = new BalderEntities())
                    {
                        // attach already existing objects to new object
                        _db.MovieHalls.Attach(screenCast.MovieHall);

                        // add new object
                        _db.ScreenCasts.Add(screenCast);
                        _db.SaveChanges();
                    }
                }
                else
                {
                    throw new ArgumentException("ScreenCast object is null");

                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Get a screen cast from its id
        /// </summary>
        /// <param name="id">Id of screen cast</param>
        /// <returns>Screen cast with Id</returns>
        /// <exception cref="Exception"></exception>
        public ScreenCast GetScreenCast(int id)
        {
            try
            {
                using (BalderEntities _db = new BalderEntities())
                {
                    return _db.ScreenCasts.Where(s => s.Id == id).Single();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
